//
//      Copyright 2011 richard collins
//
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.

#include "./time1302.h"

#define READ_WRITE_BIT { PORTE |= (1 <<PE5); PORTE &= ~(1 <<PE5); }

void Time1302_Open()
{
	//Input. CE signal must be asserted high during a read or a write. This pin has an
	//internal 40kΩ (typ) pulldown resistor to ground. Note: Previous data sheet revisions
	//referred to CE as RST. The functionality of the pin has not changed.
	PORTE &= ~(1 <<PE7); // CE pin is set low

	//Input. SCLK is used to synchronize data movement on the serial interface. This pin
	//has an internal 40kΩ (typ) pulldown resistor to ground.
	PORTE &= ~(1 <<PE5); // SCK pin is set low

	//Now the two control bits are low, make sure their direction is out.
	DDRE |= (1 <<PE7); // CE pin set to output
	DDRE |= (1 <<PE5); // SCK pin is set to output
}

void WriteCMD(u8 read,u8 address)
{
	u8 n;


	PORTE |= (1 <<PE7);// start the DS1302 bus

	//Set the data pin to out for the request command write.
	DDRE |= (1 <<PE6);

	if( read )
	{
		//Write 1 first as this will be a read request.
		PORTE |= (1 <<PE6);//Bit to send.
	}
	else
	{
		//Write 0 first as this will be a write request.
		PORTE &= ~(1 <<PE6);//Bit to send.
	}

	READ_WRITE_BIT;//send it.

	//Now write the address we want to read.
	for(n = 0 ; n < 6 ; n++ )
	{
		if( address&(1<<n) )
		{
			//Write 1
			PORTE |= (1 <<PE6);
		}
		else
		{
			//Write 0
			PORTE &= ~(1 <<PE6);
		}
		READ_WRITE_BIT;//send it.
	}

	//And now send the final bit that is always sent.
	PORTE |= (1 <<PE6);//Bit to send.
	READ_WRITE_BIT;//send it.
}

u8 ReadByte(u8 address)
{
	u8 n;

	//Send command.
	WriteCMD(1,address);

	//Now the request is sent, we need to read the response.

	//Set the data pin to in to read the result.
	DDRE &= ~(1 <<PE6);
	u8 result = 0;

	for(n = 0 ; n < 8 ; n++ )
	{
		u8 bit = (PINE & (1<<PE6))>>PE6;
		result |= bit<<n;
		READ_WRITE_BIT;//send it.
	}

	PORTE &= ~(1 <<PE7);// stop the DS1302 bus

	return result;
}



void GetDate(u8 *centry,u8 *year,u8 *month,u8 *day)
{
	*centry = 20;
	*year = ReadByte(6);
	*month = ReadByte(4);
	*day = ReadByte(3);

	//Correct for what looks like BCD encoded values, although the data sheet does not say that.
	*year = ((*year>> 4) * 10) + (*year & 0x0F);
	*month = ((*month>> 4) * 10) + (*month & 0x0F);
	*day = ((*day>> 4) * 10) + (*day & 0x0F);
}

void GetTime(u8 *hour,u8 *minute,u8 *second)
{
	*hour = ReadByte(2);
	*minute = ReadByte(1);
	*second = ReadByte(0);

	//The use a really odd way to pack the data into the bytes.
	//For example the seconds the bottom 4 bits for 0 -> 9 and the next 3 for the tens of seconds.
	*minute = ((*minute>> 4) * 10) + (*minute & 0x0F);
	*second = (((*second>> 4)&0x7) * 10) + (*second & 0x0F);

	//We make sure hour is always 24hour, can be encoded as 12 am/pm.
	*hour = (*hour & 0x1f);
	if( ((*hour)&0x80) == 0 )
	{//Not in 24 hour mode.
		if((*hour)&0x20)
		{
			*hour += 12;
		}
	}
}
